package com.kyivmohyla.kma2.client.utils;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.media.client.Audio;
import com.google.gwt.user.client.ui.*;
import com.kyivmohyla.kma2.client.gin.Injector;

public class DiscardChangesDialogBox {    //TODO Навіщо робити цей класс сінглтоном ?
    // Цей клас та DialogBoxWithMessage роблять однакові дії, ДіалогБокс заповнюжться різною інфою. Не думали використать якийсь патерн ??
    private static DiscardChangesDialogBox instance = new DiscardChangesDialogBox();
    private static Audio openSound = Audio.createIfSupported();

    public static DiscardChangesDialogBox getInstance() {
        if (instance == null) {
            instance = new DiscardChangesDialogBox();
        }
        return instance;
    }

    public void createDialogBox(final Injector injector, final GwtEvent<?> eventToOpen) {
        final DialogBox discardChanges = new DialogBox();
        discardChanges.setText("Important");

        HTML message = new HTML("You've probably made some changes or some fields are not correct.<br>Do you want close this message and save changes or discard them?<br>");
        Button closeButton = new Button("Close message", new ClickHandler() {
            public void onClick(ClickEvent event) {
                discardChanges.hide();
            }
        });
        Button discardButton = new Button("Discard Changes", new ClickHandler() {
            public void onClick(ClickEvent event) {
                injector.getEventBus().fireEvent(eventToOpen);
                discardChanges.hide();
            }
        });
        HorizontalPanel buttons = new HorizontalPanel();
        buttons.add(discardButton);
        buttons.add(closeButton);
        VerticalPanel dialogContents = new VerticalPanel();
        dialogContents.add(message);
        dialogContents.add(buttons);
        dialogContents.setStyleName("dialogContents");
        dialogContents.setCellHorizontalAlignment(message, HasHorizontalAlignment.ALIGN_CENTER);
        dialogContents.setCellVerticalAlignment(message, HasVerticalAlignment.ALIGN_MIDDLE);
        dialogContents.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
        dialogContents.setCellVerticalAlignment(buttons, HasVerticalAlignment.ALIGN_BOTTOM);
        discardChanges.setWidget(dialogContents);
        discardChanges.setAnimationEnabled(true);
        discardChanges.center();
        openSound.setSrc("sounds/Windows Notify System Generic.wav");
        openSound.play();
        discardChanges.show();
    }
}
